package com.sinodata.bsm.cicp.collector.was6;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;

import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.finder.was6.Util;
import com.sinodata.bsm.cicp.util.CollectorUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 10:01:34   liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class JVMCollector extends WasCollector {

    private static final Logger logger = Logger.getLogger(JVMCollector.class);
    private static int TIME_OUT = 15 * 60 * 1000;
    private static final String MODULE = "jvmRuntimeModule";

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String port = this.getParamMap().get("ServerPort");
        String nodeName = this.getParamMap().get("NodeName");
        String serverName = this.getParamMap().get("ServerName");
        List<ResPropertyValue> value = new ArrayList<ResPropertyValue>();

        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] proValues = new ResPropertyValue.PropValue[getPropMeta().length];
        String[] propMetas = getPropMeta();
        try {
            String respXml = WasValueBuffer.getInstance().getXml(ip, port, MODULE);
            if (respXml == null) {
                return null;
            }
            Document doc = DocumentHelper.parseText(respXml);
            Element root = doc.getRootElement();
            Node node = root.selectSingleNode("Node[@name='" + nodeName + "']");
            Node server = node.selectSingleNode("Server[@name='" + serverName + "']");
            Element cp = (Element) server.selectSingleNode("Stat");
            // JVM总内存上限
            Node e1 = cp.selectSingleNode("BoundedRangeStatistic[@ID='1']");
            String HeapSizeMax = DocumentHelper.parseText(e1.asXML()).getRootElement().attributeValue("upperBound");
            proValues[0] = ResPropertyValue.createPropValue(HeapSizeMax, propMetas[0]);
            // JVM总内存(HeapSize)
            Node e2 = cp.selectSingleNode("BoundedRangeStatistic[@ID='1']");
            String HeapSize = DocumentHelper.parseText(e2.asXML()).getRootElement().attributeValue("value");
            proValues[1] = ResPropertyValue.createPropValue(HeapSize, propMetas[1]);
            // JVM已用内存
            Node e3 = cp.selectSingleNode("CountStatistic[@ID='3']");
            String UsedMemory = DocumentHelper.parseText(e3.asXML()).getRootElement().attributeValue("count");
            proValues[2] = ResPropertyValue.createPropValue(UsedMemory, propMetas[2]);
            // JVM运行的时间
            Node e4 = cp.selectSingleNode("CountStatistic[@ID='4']");
            String UpTime = DocumentHelper.parseText(e4.asXML()).getRootElement().attributeValue("count");
            proValues[3] = ResPropertyValue.createPropValue(UpTime, propMetas[3]);
            // JVM占用cpu率
            String ProcessCpuUsage;
            if (null == cp.selectSingleNode("CountStatistic[@ID='5']")) {
                ProcessCpuUsage = "0";
            } else {
                Node e5 = cp.selectSingleNode("CountStatistic[@ID='5']");
                ProcessCpuUsage = DocumentHelper.parseText(e5.asXML()).getRootElement().attributeValue("count");
                //保留小数点后两位
                Double pused = Double.parseDouble(ProcessCpuUsage);
                float f = Math.round(pused * 100);
                ProcessCpuUsage = "" + f / 100;
            }
            proValues[4] = ResPropertyValue.createPropValue(ProcessCpuUsage, propMetas[4]);
            propertyValueX.setValues(proValues);
            value.add(propertyValueX);

        } catch (Exception e) {
            logger.error("failed to collect by JVMCollector", e);
            throw new CollectorException(e);
        }
        return value;
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "服务器端口", "服务器名", "节点名" };
        return new String[] { "ServerPort", "ServerName", "NodeName" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "JVM总内存上限", "JVM总内存(HeapSize)", "JVM已用内存", "JVM运行的时间", "JVM占用cpu率" };
        //return new String[] { "TotalJVMUpperLimit", "TotalJVMMemory", "JVMUsedMemory", "JVMRuntime", "JVMOccupyCPURatio" };
        return new String[] { "$1", "$2", "$3", "$4", "$5" };

    }

}